#!/usr/bin/env/ python
# -*- coding: utf-8 -*-

"""
@author: super
@software: PyCharm
@file: rest_cloent
@time: 2022/1/27 11:15
"""
import requests
import json as complexjson
from commonon.logger import logger

class RestClient():

    def __init__(self, api_root_url):
        '''
        :param api_root_url: 请求路径
        '''
        self.api_root_url = api_root_url

    def get(self, url, **kwargs) :
        '''get方法'''
        resp = self.request(url, "GET", **kwargs)
        logger.info("返回结果：{0}".format(resp.json()))
        return resp

    def post(self, url, data = None, json = None, **kwargs) :
        '''post方法'''
        resp = self.request(url, "POST", data, json, **kwargs)
        logger.info("返回结果：{0}".format(resp.json()))
        return resp

    def put(self, url, data = None, json = None, **kwargs) :
        '''put方法'''
        resp = self.request(url, "PUT", data, json, **kwargs)
        logger.info("返回结果：{0}".format(resp.json()))
        return resp

    def delete(self, url, **kwargs) :
        '''delete方法'''
        resp = self.request(url, "DELETE", **kwargs)
        logger.info("返回结果：{0}".format(resp.json()))
        return resp

    def patch(self, url, data = None, **kwargs) :
        '''patch方法'''
        resp = self.request(url, "PATCH", data, **kwargs)
        logger.info("返回结果：{0}".format(resp.json()))
        return resp

    def request(self, url, method, data = None, json = None, **kwargs) :
        '''requesr方法'''
        url = self.api_root_url + url
        headers = dict(**kwargs).get("headers")
        params = dict(**kwargs).get("params")
        files = dict(**kwargs).get("params")
        cookies = dict(**kwargs).get("params")
        self.request_log(url, method, data, json, params, headers, files, cookies)
        if method == "GET" :
            return requests.get(url, **kwargs)
        if method == "POST" :
            return requests.post(url, data = data, json = json, **kwargs)
        if method == "PUT" :
            return requests.put(url, data = data, json = json, **kwargs)
        if method == "DELETE" :
            return requests.delete(url, **kwargs)
        if method == "PATCH" :
            if json :
                data = complexjson.dumps(json)
            return requests.patch(url, data, **kwargs)

    def request_log(self, url, method, data=None, json=None, params=None, headers=None, files=None, cookies=None, **kwargs):
        '''封装log日志方法'''
        logger.info("接口请求地址 ==>> {}".format(url))
        logger.info("接口请求方式 ==>> {}".format(method))
        # Python3中，json在做dumps操作时，会将中文转换成unicode编码，因此设置 ensure_ascii=False
        logger.info("接口请求头 ==>> {}".format(complexjson.dumps(headers, indent=4, ensure_ascii=False)))
        logger.info("接口请求 params 参数 ==>> {}".format(complexjson.dumps(params, indent=4, ensure_ascii=False)))
        logger.info("接口请求体 data 参数 ==>> {}".format(complexjson.dumps(data, indent=4, ensure_ascii=False)))
        logger.info("接口请求体 json 参数 ==>> {}".format(complexjson.dumps(json, indent=4, ensure_ascii=False)))
        logger.info("接口上传附件 files 参数 ==>> {}".format(files))
        logger.info("接口 cookies 参数 ==>> {}".format(complexjson.dumps(cookies, indent=4, ensure_ascii=False)))